package com.freesky.biz.rights.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freesky.biz.rights.domain.RightsBusiEntity;
import com.freesky.biz.rights.domain.RightsWhiteUserEntity;
import com.freesky.biz.rights.service.IRightsBusiService;
import com.freesky.biz.rights.service.IRightsWhiteUserService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.QueryWrapperUtils;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.SeqUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 权益白名单管理
 */
@RestController
@RequestMapping("/rights/whiteuser")
public class RightsWhiteUserController extends BaseController {

    @Autowired
    private IRightsWhiteUserService rightsWhiteUserService;

    /**
     * 分页获取权益业务
     */
    @PreAuthorize("@ss.hasPermi('rights:whiteuser:list')")
    @GetMapping("/page")
    public TableDataInfo page(RightsWhiteUserEntity rightsWhiteUser) {
        QueryWrapper<RightsWhiteUserEntity> queryWrapper = QueryWrapperUtils.toSimpleQuery(rightsWhiteUser);
        queryWrapper.eq("del_flag",0);
        queryWrapper.orderByDesc("create_time");
        IPage<RightsWhiteUserEntity> pageList = rightsWhiteUserService.page(plusPage(),queryWrapper);
        return getDataTable(pageList);
    }

    /**
     * 根据id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('rights:whiteuser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return success(rightsWhiteUserService.getById(id));
    }



    /**
     * 新增权益业务
     */
    @PreAuthorize("@ss.hasPermi('rights:whiteuser:add')")
    @Log(title = "权益白名单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody RightsWhiteUserEntity rightsWhiteUser) {
        rightsWhiteUser.setId(SeqUtil.getSeqId());
        rightsWhiteUser.setDelFlag("0");
        rightsWhiteUser.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        rightsWhiteUser.setCreateTime(new Date());
        rightsWhiteUser.setUpdateTime(new Date());
        return toAjax(rightsWhiteUserService.save(rightsWhiteUser));
    }

    /**
     * 修改权益业务
     */
    @PreAuthorize("@ss.hasPermi('rights:whiteuser:edit')")
    @Log(title = "权益白名单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RightsWhiteUserEntity rightsWhiteUse) {
        rightsWhiteUse.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        rightsWhiteUse.setUpdateTime(new Date());
        return toAjax(rightsWhiteUserService.updateById(rightsWhiteUse));
    }

    /**
     * 删除权益业务
     */
    @PreAuthorize("@ss.hasPermi('rights:whiteuser:remove')")
    @Log(title = "权益白名单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        LambdaUpdateWrapper<RightsWhiteUserEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(RightsWhiteUserEntity::getId,ids);
        updateWrapper.set(RightsWhiteUserEntity::getDelFlag,"1");
        updateWrapper.set(RightsWhiteUserEntity::getUpdateTime,new Date());
        updateWrapper.set(RightsWhiteUserEntity::getUpdateBy,String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        return toAjax(rightsWhiteUserService.update(updateWrapper));
        //return toAjax(rightsWhiteUserService.up(ids.toArray(new String[0])));
    }
}
